package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.CompanyResult;
import com.itheima.common.Result;
import com.itheima.common.SubjectResult;
import com.itheima.mapper.BsUserMapper;
import com.itheima.mapper.DirectorysMapper;
import com.itheima.mapper.HmSubjectsMapper;
import com.itheima.pojo.BsUser;
import com.itheima.pojo.hm.HmSubjects;
import com.itheima.pojo.hm.HmSubjectsDirectorys;
import com.itheima.pojo.hm.HmSubjectsTags;
import com.itheima.service.DirectorysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/*
 *@Parm
 *@ClassName DirectorysServiceImpl
 *@Date 2022/1/10 15:07
 */
@Service
public class DirectorysServiceImpl implements DirectorysService {

    @Autowired
    private DirectorysMapper directorysMapper;
    @Override
    public CompanyResult updateById(HmSubjectsDirectorys directorys) {
        int i = directorysMapper.updateById(directorys);
        if(i <= 0){
            return new CompanyResult(false);
        }
        return new CompanyResult(true);
    }
    @Autowired
    private BsUserMapper bsUserMapper;

    @Autowired
    private HmSubjectsMapper hmSubjectsMapper;
    @Override
    public Result findPage(Integer page, Integer pagesize, String directoryName, Integer state, Integer subjectID) {
        IPage<HmSubjectsDirectorys> p = new Page<>(page,pagesize);
        LambdaQueryWrapper<HmSubjectsDirectorys> wrapper = new LambdaQueryWrapper<>();
        if(directoryName != null){
            wrapper.like(HmSubjectsDirectorys::getDirectoryName,directoryName);
        }
        if(state != null){
            wrapper.eq(HmSubjectsDirectorys::getState,state);
        }
        if(subjectID != null){
            wrapper.eq(HmSubjectsDirectorys::getSubjectID,subjectID);
        }
        IPage<HmSubjectsDirectorys> Ipage = directorysMapper.selectPage(p, wrapper);
        Integer userId = (Integer) redisTemplate.opsForValue().get("userId");
        BsUser bsUser = bsUserMapper.selectById(userId);
        List<HmSubjectsDirectorys> hmSubjectsDirectorys = Ipage.getRecords();
        for (HmSubjectsDirectorys directorys : hmSubjectsDirectorys) {
            directorys.setUsername(bsUser.getUsername());
            HmSubjects hmSubjects = hmSubjectsMapper.selectById(directorys.getSubjectID());
            directorys.setSubjectName(hmSubjects.getSubjectName());
        }

        Result result = new Result();
        result.setCounts(Ipage.getTotal());
        result.setPagesize(pagesize);
        result.setPage(page);
        result.setItems(Ipage.getRecords());
        result.setPages(Ipage.getPages());
        return result;
    }

    @Override
    public CompanyResult deleteById(Integer id) {
        int i = directorysMapper.deleteById(id);
        if(i <= 0){
            return new CompanyResult(false);
        }
        return new CompanyResult(true);
    }

    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public CompanyResult add(HmSubjectsDirectorys directorys) {
        directorys.setAddDate(LocalDateTime.now());
        String userId = (String) redisTemplate.opsForValue().get("userId");
        directorys.setState(1);
        directorys.setTotals(0);
        directorys.setCreatorID(Integer.parseInt(userId));
        int insert = directorysMapper.insert(directorys);
        if(insert <= 0){
            return new CompanyResult(false);
        }
        return new CompanyResult(true);
    }

    @Override
    public CompanyResult updateByState(Integer id, Integer state) {
        HmSubjectsDirectorys directorys = new HmSubjectsDirectorys();
        directorys.setId(id);
        directorys.setState(state);
        int i = directorysMapper.updateById(directorys);
        if(i <= 0){
            return new CompanyResult(false);
        }
        return new CompanyResult(true);
    }

    @Override
    public HmSubjectsDirectorys findOne(Integer id) {
        HmSubjectsDirectorys directorys = directorysMapper.selectById(id);
        return directorys;
    }

    @Override
    public List<SubjectResult> SimpleDirectory(Integer subjectID) {
        LambdaQueryWrapper<HmSubjectsDirectorys> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(HmSubjectsDirectorys::getSubjectID,subjectID);
        List<HmSubjectsDirectorys> hmSubjectsDirectorys = directorysMapper.selectList(wrapper);
        List<SubjectResult> resultList = new ArrayList<>();
        for (HmSubjectsDirectorys hmSubjectsDirectory : hmSubjectsDirectorys) {
            SubjectResult subjectResult = new SubjectResult();
            subjectResult.setValue(hmSubjectsDirectory.getId());
            subjectResult.setLabel(hmSubjectsDirectory.getDirectoryName());
            resultList.add(subjectResult);
        }
        return resultList;
    }
}
